package demo.practice.stack;

import java.util.Stack;

public class P1047 {
    public String removeDuplicates(String S) {
        if(S==null)
            return null;
        if(S.equals(""))
            return "";
        Stack<Character> stack= new Stack<>();
        for(char c :S.toCharArray()){
            if(stack.size()==0 || stack.peek()!=c)
                stack.add(c);
            else{
                stack.pop();
            }
        }
        if(stack.size()==0)
            return "";
        char[] cs= new char[stack.size()];
        for(int i=cs.length-1;i>=0;i--){
            cs[i]=stack.pop();
        }
        return new String(cs);
    }
}
